# AMAR ET AL. - COUNTERING MISINFORMATION EARLY (2025)
## REPLICATION FILE: 19_teacher_analysis.R
### This script creates a balance table for teachers.
# ----
# Create table ----
# Import data
df <- read_csv("./data/raw/teachers/teachers_balance.csv")

# Generate LaTeX summary statistics table
generate_latex_table <- function(data) {
  
  # Helper function for proportions
  get_prop <- function(data, var, value, condition) {
    filtered_data <- data %>% filter(!!sym(condition) == 1)
    if (nrow(filtered_data) == 0) return("0.000")
    prop <- sum(filtered_data[[var]] == value, na.rm = TRUE) / nrow(filtered_data)
    return(sprintf("%.3f", prop))
  }
  
  get_prop0 <- function(data, var, value, condition) {
    filtered_data <- data %>% filter(!!sym(condition) == 0)
    if (nrow(filtered_data) == 0) return("0.000")
    prop <- sum(filtered_data[[var]] == value, na.rm = TRUE) / nrow(filtered_data)
    return(sprintf("%.3f", prop))
  }
  
  # Calculate statistics
  age_media1 <- data %>% filter(media == 1) %>% summarise(mean = mean(age, na.rm = TRUE), sd = sd(age, na.rm = TRUE))
  age_media0 <- data %>% filter(media == 0) %>% summarise(mean = mean(age, na.rm = TRUE), sd = sd(age, na.rm = TRUE))
  
  # Count observations
  n_media1 <- sum(data$media == 1, na.rm = TRUE)
  n_media0 <- sum(data$media == 0, na.rm = TRUE)
  
  # Create LaTeX table
  latex_content <- c(
    "\\begin{table}[ht]",
    " \\caption{Summary statistics of teachers, by treatment condition} ",
    "\\addlinespace  ",
    "\\centering ",
    "\\begin{tabular}{lcc} ",
    "\\toprule ",
    "& \\multicolumn{1}{c}{Media Literacy} & \\multicolumn{1}{c}{Spoken English} \\\\ \\midrule ",
    " Statistic & Mean (SD)/Prop. & Mean (SD)/Prop. \\\\  \\midrule ",
    paste0("Age & ", sprintf("%.3f", age_media1$mean), " (", sprintf("%.2f", age_media1$sd), ")& ", 
           sprintf("%.3f", age_media0$mean), " (", sprintf("%.2f", age_media0$sd), ") \\\\ \\addlinespace "),
    "Gender & &  \\\\  ",
    paste0("\\quad Female  & ", get_prop(data, "gender", "Female", "media"), " & ", 
           get_prop0(data, "gender", "Female", "media"), "  \\\\  "),
    paste0("\\quad Male  & ", get_prop(data, "gender", "Male", "media"), " & ", 
           get_prop0(data, "gender", "Male", "media"), "  \\\\ "),
    "Caste Category & &  \\\\  ",
    paste0("\\quad Adivasi ST & ", get_prop(data, "caste", "Adivasi ST", "media"), " & ", 
           get_prop0(data, "caste", "Adivasi ST", "media"), "  \\\\  "),
    paste0("\\quad Dalit SC & ", get_prop(data, "caste", "Dalit SC", "media"), " & ", 
           get_prop0(data, "caste", "Dalit SC", "media"), "  \\\\  "),
    paste0("\\quad General / Upper & ", get_prop(data, "caste", "General / Upper", "media"), " & ", 
           get_prop0(data, "caste", "General / Upper", "media"), "  \\\\ "),
    paste0("\\quad OBC & ", get_prop(data, "caste", "OBC", "media"), " & ", 
           get_prop0(data, "caste", "OBC", "media"), "  \\\\  "),
    paste0("\\quad Prefer not to say & ", get_prop(data, "caste", "Prefer not to say", "media"), " & ", 
           get_prop0(data, "caste", "Prefer not to say", "media"), "  \\\\ \\addlinespace  "),
    "Religion & &  \\\\  ",
    paste0("\\quad Hindu & ", get_prop(data, "religion", "Hindu", "media"), " & ", 
           get_prop0(data, "religion", "Hindu", "media"), "  \\\\  "),
    paste0("\\quad Muslim & ", get_prop(data, "religion", "Muslim", "media"), " & ", 
           get_prop0(data, "religion", "Muslim", "media"), "  \\\\  "),
    paste0("\\quad Other & ", get_prop(data, "religion", "Other", "media"), " & ", 
           get_prop0(data, "religion", "Other", "media"), "  \\\\ \\addlinespace  "),
    "Education - School Type & &  \\\\  ",
    paste0("\\quad Government  & ", get_prop(data, "school_type", "Government", "media"), " & ", 
           get_prop0(data, "school_type", "Government", "media"), "  \\\\  "),
    paste0("\\quad Private  & ", get_prop(data, "school_type", "Private", "media"), " & ", 
           get_prop0(data, "school_type", "Private", "media"), "  \\\\  "),
    paste0("\\quad Other & ", get_prop(data, "school_type", "Other", "media"), " & ", 
           get_prop0(data, "school_type", "Other", "media"), "  \\\\ \\addlinespace  "),
    " \\midrule  ",
    paste0("     N	 & ", n_media1, " & ", n_media0, " & 	 "),
    "\\bottomrule",
    "\\end{tabular}",
    "\\label{tab:teachers}",
    "\\end{table}"
  )
  
  return(latex_content)
}

# Generate and write LaTeX table
dir.create("./output/tables", recursive = TRUE, showWarnings = FALSE)
latex_table <- generate_latex_table(df)
writeLines(latex_table, "./output/tables/tab_teachers_balance.tex")

# END of 19_teacher_analysis.R ----
